package com.zzy.furns.web;

import com.zzy.furns.domain.Furn;
import com.zzy.furns.domain.Page;
import com.zzy.furns.service.FurnService;
import com.zzy.furns.service.impl.FurnServiceImpl;
import com.zzy.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomerServlet extends BasicServlet{

    private FurnService furnService = new FurnServiceImpl();

    private void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"));
        Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"));
        if (pageNo > 0 && pageSize > 0){
            Page<Furn> page = furnService.page(pageNo, pageSize);
            //使用请求转发前都要思考是否可以使用请求转发
            req.setAttribute("page",page);
            req.getRequestDispatcher("/views/customer/index.jsp").forward(req,resp);
            return;
        }
        System.out.println("pageNo 或 pageSize输入转换错误");
    }

    private void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"));
        Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"));
        String pageName = req.getParameter("pageName");

        if (pageNo > 0 && pageSize > 0){
            Page<Furn> page = furnService.pageByName(pageName, pageNo, pageSize);
            req.setAttribute("pageName",pageName);
            req.setAttribute("page",page);
            req.getRequestDispatcher("/views/customer/index.jsp").forward(req,resp);
            return;
        }
        System.out.println("pageNo 或 pageSize输入转换错误");
    }
}
